import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../../resource/language/generated/l10n.dart';
import 'empty_view/jr_empty_view.dart';

mixin JRRefreshListMixin {
  String? errorMsg = '';
  List? data;
  NullableIndexedWidgetBuilder itemBuilder =
      (context, index) => const SizedBox();

  Future<void> loadData() async {}

  Widget get errorWidget => JREmptyView(
      errorType: ErrorType.noData, desc: errorMsg, onTap: () => loadData());

  Widget get emptyWidget => JREmptyView(
      errorType: ErrorType.noData,
      desc: T.current.no_data_click_refresh,
      onTap: () => loadData());

  Widget get loadingWidget => const Center(child: CupertinoActivityIndicator());

  Widget get listWidget => RefreshIndicator(
        onRefresh: loadData,
        child: ListView.builder(
            physics: const AlwaysScrollableScrollPhysics(
                parent: BouncingScrollPhysics()),
            itemCount: data?.length ?? 0,
            itemBuilder: itemBuilder),
      );

  Widget get root {
    if (errorMsg?.isNotEmpty ?? false) {
      return errorWidget;
    } else if (data == null) {
      return loadingWidget;
    } else if (data!.isEmpty) {
      return emptyWidget;
    } else {
      return listWidget;
    }
  }
}
